package com.wf.smartcomm.server.admin.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.UnitMachine;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.config.UmConfig;
import com.wf.smartcomm.server.model.PushMessage;
import com.wf.smartcomm.server.push.Pusher;
import com.wf.smartcomm.server.service.UmConfigService;
import com.wf.smartcomm.server.service.UnitMachService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.PushUtils;
import com.wf.smartcomm.server.util.StringUtil;

@Controller
@Scope("prototype")
public class UmConfigAction extends BaseAction implements ModelDriven<UmConfig>{

	private UmConfig config = new UmConfig();
	
	@Resource
	private UmConfigService configServiceImpl;
	@Resource
	private UserService userServiceImpl;
	@Resource
	private UnitMachService umServiceImpl;
	
	public void save() throws Exception{
		configServiceImpl.save(config);
		printJsonData(200);
		if (!StringUtil.isEmpty(config.getReportTime())) {
			umServiceImpl.list(page);
			List<UnitMachine> ums = (List<UnitMachine>) page.getDataList();
			for (UnitMachine unitMachine : ums) {
				unitMachine.setTimeSpan(config.getReportTime()+"000");
				umServiceImpl.update(unitMachine);
			}
		}
		userServiceImpl.listUmUser(page);
		List<User> users = (List<User>) page.getDataList();
		Pusher pusher = PushUtils.getInstance();
		PushMessage msg = new PushMessage();
		msg.setPushType(ConstantUtils.PUSH_TYPE_CONFIG_MSG);
		msg.setData(config.getObjectId());
		for (User user : users) {
			pusher.push0x20Message(user.getPushId().getBytes(), outGson.toJson(msg).getBytes());
		}
		pusher.close();
	}
	
	public void reset() throws Exception{
		config.setReset("1");
		configServiceImpl.save(config);
		printJsonData(200);
		userServiceImpl.listUmUser(page);
		List<User> users = (List<User>) page.getDataList();
		Pusher pusher = PushUtils.getInstance();
		PushMessage msg = new PushMessage();
		msg.setPushType(ConstantUtils.PUSH_TYPE_RESET_MSG);
		for (User user : users) {
			pusher.push0x20Message(user.getPushId().getBytes(), outGson.toJson(msg).getBytes());
		}
		pusher.close();
	}
	
	public void restart() throws Exception{
		config.setRestart("1");
		configServiceImpl.save(config);
		printJsonData(200);
		userServiceImpl.listUmUser(page);
		List<User> users = (List<User>) page.getDataList();
		Pusher pusher = PushUtils.getInstance();
		PushMessage msg = new PushMessage();
		msg.setPushType(ConstantUtils.PUSH_TYPE_RESTART_MSG);
		for (User user : users) {
			pusher.push0x20Message(user.getPushId().getBytes(), outGson.toJson(msg).getBytes());
		}
		pusher.close();
	}
	
	@Override
	public UmConfig getModel() {
		// TODO Auto-generated method stub
		return config;
	}
}
